//
//  PhotoBrowserCell.swift
//  SinaWeiBo
//
//  Created by ZKJ on 2017/11/21.
//  Copyright © 2017年 ZKJ. All rights reserved.
//

import UIKit
import SDWebImage

protocol PhotoBrowserCellDelegate : NSObjectProtocol {
    func imageViewClick()
}

class PhotoBrowserCell: UICollectionViewCell {
    
    // MARK:- 定义属性
    var picUrl : NSURL? {
        didSet {
            setUpContent(picUrl: picUrl)
        }
    }
    
    var delegate : PhotoBrowserCellDelegate?
    
    // MARK:- 懒加载属性
    fileprivate lazy var scrollView : UIScrollView = UIScrollView()
    lazy var imgView : UIImageView = UIImageView()
    fileprivate lazy var progressView : ProgressView = ProgressView()
    
    // MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PhotoBrowserCell {
    fileprivate func setUpUI() {
        contentView.addSubview(scrollView)
        scrollView.addSubview(imgView)
        scrollView.frame = contentView.bounds
        scrollView.frame.size.width -= 20
        imgView.contentMode = UIViewContentMode.scaleAspectFill
        
        // MARK:- 添加进度视图
        progressView.bounds = CGRect(x: 0, y: 0, width: 60, height: 60)
        progressView.center = CGPoint(x: UIScreen.main.bounds.width * 0.5, y: UIScreen.main.bounds.height * 0.5)
        progressView.isHidden = true;
        progressView.backgroundColor = UIColor.clear
        contentView.addSubview(progressView)
        
        // MARK:- 监听imageView的点击
        let tap = UITapGestureRecognizer(target: self, action: #selector(PhotoBrowserCell.tapImageView))
        imgView.isUserInteractionEnabled = true
        imgView.addGestureRecognizer(tap)
    }
}

extension PhotoBrowserCell {
    fileprivate func setUpContent(picUrl : NSURL?) {
        guard let picUrl = picUrl else {
            return
        }
        
        let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: picUrl.absoluteString)
        let width = UIScreen.main.bounds.width
        let height = width / (image?.size.width)! * (image?.size.height)!
        var y : CGFloat = 0
        if height > UIScreen.main.bounds.height {
            y = 0
        } else {
            y = (UIScreen.main.bounds.height - height) * 0.5
        }
        
        imgView.frame = CGRect(x: 0, y: y, width: width, height: height)
        progressView.isHidden = false
        imgView.sd_setImage(with: getBigImageURL(smallUrl: picUrl) as URL, placeholderImage: image, options: [], progress: { (current, total, _) in
            self.progressView.progress = CGFloat(current) / CGFloat(total)
        }) { (_, _ , _ , _ ) in
            self.progressView.isHidden = true
        }
        scrollView.contentSize = CGSize(width: 0, height: height)
    }
    
    // MARK:- 小图换大图
    fileprivate func getBigImageURL(smallUrl : NSURL) -> NSURL {
        let smallUrlString = smallUrl.absoluteString
        let bigUrlString = smallUrlString?.replacingOccurrences(of: "thumbnail", with: "bmiddle")
        return NSURL(string: bigUrlString!)!
    }
}

extension PhotoBrowserCell {
    @objc fileprivate func tapImageView() {
        delegate?.imageViewClick()
    }
}



